package com.heima.leadnews.user.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.leadnews.user.mapper.ApUserMapper;
import com.heima.leadnews.user.mapper.ApUserRealNameMapper;
import com.heima.leadnews.user.service.ApUserRealNameService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dto.ApUserAuthDto;
import com.heima.model.user.pojo.ApUser;
import com.heima.model.user.pojo.ApUserRealName;
import com.heima.model.wemedia.pojos.WmUser;
import com.itheima.leadnews.feign.wemedia.WemediaFeign;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.Acceleration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ApUserRealNameServiceImpl extends ServiceImpl<ApUserRealNameMapper, ApUserRealName> implements ApUserRealNameService {

    @Autowired
    private ApUserMapper apUserMapper;

    @Autowired
    private WemediaFeign wemediaFeign;

    /**
     * 审核分页查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(ApUserAuthDto dto) {
        //分页查询
        Page<ApUserRealName> page = new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<ApUserRealName> queryWrapper = new LambdaQueryWrapper<>();
        //设置条件
        queryWrapper.eq(dto.getStatus() != null,ApUserRealName::getStatus,dto.getStatus());
        queryWrapper.orderByDesc(ApUserRealName::getCreatedTime);
        page(page,queryWrapper);
        //封装数据
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 审核失败
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFail(ApUserAuthDto dto) {
        //审核失败
        LambdaUpdateWrapper<ApUserRealName> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ApUserRealName::getId,dto.getId());
        updateWrapper.set(StringUtils.isNotEmpty(dto.getMsg()),ApUserRealName::getReason,dto.getMsg())
                .set(ApUserRealName::getStatus,ApUserRealName.AUTH_FAIL);
        update(updateWrapper);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 审核通过
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public ResponseResult authPass(ApUserAuthDto dto) {
        //审核通过,修改状态为9
        LambdaUpdateWrapper<ApUserRealName> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ApUserRealName::getId,dto.getId());
        //修改状态为9
        updateWrapper.set(ApUserRealName::getStatus,ApUserRealName.AUTH_PASS);
        update(updateWrapper);
        //开通自媒体账号,查询ap_user数据库获取用户信息
        ApUserRealName apUserRealName = getById(dto.getId());
        ApUser user = apUserMapper.selectById(apUserRealName.getUserId());
        user.setIdentityAuthentication(true);
        user.setCertification(true);
        apUserMapper.updateById(user);
        //远程调用自媒体微服务完成认证app端用户信息保存
        WmUser wmUser = new WmUser();
        wmUser.setApUserId(user.getId());
        wmUser.setName(user.getName());
        wmUser.setPassword(user.getPassword());
        wmUser.setSalt(user.getSalt());
        wmUser.setPhone(user.getPhone());
        wmUser.setStatus(9);
        wemediaFeign.pass(wmUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
